//*********************************************************************** // PROGRAM NAME: javaex.txt // Sample java program to talk to instrument via SCPI-over-sockets // This program requires Java version 1.1 or later. // Save this code as ScpiSockTest.java // Complile by typing: javac ScpiSockTest.java // Run by typing: java ScpiSockTest // The signal generator is set for 1 gHz and queried for it's id string //*********************************************************************** import java.io.*; import java.net.*; class ScpiSockTest { public static void main(String[] args) { String instrumentName = "xxxxx"; // Put your instrument's name here try { Socket t = new Socket(instrumentName,5025); // Connect to instrument // Setup read/write mechanism BufferedWriter out = new BufferedWriter( new OutputStreamWriter(t.getOutputStream())); BufferedReader in = new BufferedReader( new InputStreamReader(t.getInputStream())); System.out.println("Setting frequency to 1 Ghz..."); out.write("freq 1Ghz\n"); // Set frequency out.flush(); System.out.println("Waiting for source to settle..."); out.write("*opc?\n"); // Wait for complete out.flush(); String opcResponse = in.readLine(); if (!opcResponse.equals("1")) { System.err.println("Invalid response to '*OPC?'!"); System.exit(1); } System.out.println("Retrieving instrument ID..."); out.write("*idn?\n"); // Query the id string out.flush(); String idnResponse = in.readLine(); // Read the id string // Pring the id string System.out.println("Instrument ID: " + idnResponse); } catch (IOException e) { System.out.println("Error" + e); } } }